package com.software.loginregister.adapter;

import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.core.content.ContextCompat;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.DataSource;
import com.bumptech.glide.load.engine.GlideException;
import com.bumptech.glide.request.RequestListener;
import com.bumptech.glide.request.target.Target;
import com.software.loginregister.api.CommentApi;
import com.software.loginregister.entity.Comment;

import com.software.loginregister.R;
import com.software.loginregister.entity.Like;
import com.software.loginregister.util.RetrofitClient;

import java.text.SimpleDateFormat;
import java.util.List;
import java.util.Locale;

public class CommentAdapter extends RecyclerView.Adapter<CommentAdapter.CommentViewHolder> {
    private List<Comment> comments;
    private SimpleDateFormat dateFormat;
    private OnCommentActionListener listener;
    private Context context;

    public interface OnCommentActionListener {
        void onLikeClick(Comment comment, int position);

        void onUserClick(Integer userId);
    }

    public void setComments(List<Comment> comments) {
        this.comments = comments;
        notifyDataSetChanged();
    }

    public CommentAdapter(Context context, List<Comment> comments, OnCommentActionListener listener) {
        this.context = context;
        this.comments = comments;
        this.listener = listener;
        this.dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm", Locale.getDefault());
    }



    @NonNull
    @Override
    public CommentViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.item_comment, parent, false);
        return new CommentViewHolder(view);
    }



    @Override
    public void onBindViewHolder(@NonNull CommentViewHolder holder, int position) {
        Comment comment = comments.get(position);
        // 设置用户头像
        if (!TextUtils.isEmpty(comment.getAvatar())) {
            Glide.with(context)
                    .load(comment.getAvatar())
                    .placeholder(R.mipmap.ic_aicooking)
                    .error(R.mipmap.ic_aicooking) // 添加错误占位图
                    .listener(new RequestListener<Drawable>() {
                        @Override
                        public boolean onLoadFailed(@Nullable GlideException e, Object model, Target<Drawable> target, boolean isFirstResource) {
                            Log.e("CommentAdapter", "Avatar load failed for URL: " + comment.getAvatar(), e);
                            return false;
                        }

                        @Override
                        public boolean onResourceReady(Drawable resource, Object model, Target<Drawable> target, DataSource dataSource, boolean isFirstResource) {
                            Log.d("CommentAdapter", "Avatar loaded successfully: " + comment.getAvatar());
                            return false;
                        }
                    })
                    .circleCrop()
                    .into(holder.ivUserAvatar);
        } else {
            holder.ivUserAvatar.setImageResource(R.mipmap.ic_aicooking);
        }
        // 设置用户名
        holder.tvUserName.setText(comment.getUserName());

        // 设置评论内容
        holder.tvContent.setText(comment.getContent());

        // 设置评论时间
        holder.tvTime.setText(dateFormat.format(comment.getCreatedAt()));

        // 设置点赞数
        holder.tvLikeCount.setText(String.valueOf(comment.getLikesCount()));

        // 点击头像或用户名跳转到用户主页
        View.OnClickListener userClickListener = v -> {
            if (listener != null) {
                listener.onUserClick(comment.getUserId());
            }
        };
        holder.ivUserAvatar.setOnClickListener(userClickListener);
        holder.tvUserName.setOnClickListener(userClickListener);

        // 点赞功能
        holder.llLike.setOnClickListener(v -> {
            if (listener != null) {
                listener.onLikeClick(comment, position);
            }
        });
    }

    @Override
    public int getItemCount() {
        return comments.size();
    }

    public void addComment(Comment comment) {
        comments.add(0, comment);
        notifyItemInserted(0);
    }

    public void updateComment(Comment comment, int position) {
        comments.set(position, comment);
        notifyItemChanged(position);
    }

    static class CommentViewHolder extends RecyclerView.ViewHolder {
        ImageView ivUserAvatar;
        TextView tvUserName;
        TextView tvContent;
        TextView tvTime;
        LinearLayout llLike;
        ImageView ivLike;
        TextView tvLikeCount;

        CommentViewHolder(View itemView) {
            super(itemView);
            ivUserAvatar = itemView.findViewById(R.id.iv_user_avatar);
            tvUserName = itemView.findViewById(R.id.tv_user_name);
            tvContent = itemView.findViewById(R.id.tv_content);
            tvTime = itemView.findViewById(R.id.tv_time);
            llLike = itemView.findViewById(R.id.ll_like);
            ivLike = itemView.findViewById(R.id.iv_like);
            tvLikeCount = itemView.findViewById(R.id.tv_like_count);
        }
    }
}